const {
	default: axios
} = require('axios');
const ip = require('../models/ip.js');
const Ip = require('../models/ip.js');
const apiKey_tx = `自己的腾讯地图key`;
const apiKey_gd = `自己的高德地图key`;
//
/**
 * 获取ip信息
 * @param {Object} clientIP 用户ip
 */
async function getIpInfo(clientIP) {
	try {
		//
		// const result = await axios.get(`https://ipapi.co/${clientIP}/json`);
		const result = await axios.get(`https://apis.map.qq.com/ws/location/v1/ip`, {
			params: {
				ip: clientIP,
				key: apiKey_tx
			}
		});
		return result;
	} catch (error) {
		throw new Error(error.message);
	}
}
/**
 * 精确获取ip地址
 */
async function getIpInfoPlus(params) {
	try {
		//
		// 解构请求参数
		const {
			latitude = '', longitude = ''
		} = params;

		// 检查必要参数是否为空
		if (!latitude || !longitude) {
			throw new Error('缺少必要参数');
		}
		const result = await axios.get(`https://apis.map.qq.com/ws/geocoder/v1/`, {
			params: {
				location: `${latitude},${longitude}`,
				key: apiKey_tx
			}
		});
		return result;
	} catch (error) {
		throw new Error(error.message);
	}
}
/**
 * 关键词提示
 */
async function suggestion(params) {
	try {
		//
		// 解构请求参数
		const {
			keyword = '', region = ''
		} = params;

		// 检查必要参数是否为空
		if (!keyword) {
			throw new Error('缺少必要参数');
		}
		const result = await axios.get(`https://apis.map.qq.com/ws/place/v1/suggestion`, {
			params: {
				keyword: keyword,
				region: region,
				key: apiKey_tx
			}
		});
		return result;
	} catch (error) {
		throw new Error(error.message);
	}
}
/**
 * 路线规划
 */
async function getRoutePlanning(params) {
	// 解构请求参数
	const {
		start = '',
			end = '',
			type = 'walking',
			city = ''
	} = params;
	// 检查必要参数是否为空
	if (!start || !end || !type) {
		throw new Error('缺少必要参数');
	}
	// 定义交通方式到URL前缀的映射对象
	const urlPrefixMap = {
		walking: 'https://restapi.amap.com/v3/direction/',
		driving: 'https://restapi.amap.com/v3/direction/',
		bicycling: 'https://restapi.amap.com/v4/direction/',
		transit: 'https://restapi.amap.com/v3/direction/'
	};
	// 检查交通方式是否在映射对象中
	if (!(type in urlPrefixMap)) {
		throw new Error('不支持的交通方式');
	}
	// 获取对应的URL前缀
	const urlPrefix = urlPrefixMap[type];
	//
	let urls = `${urlPrefix}${type}`;
	//
	if (type === 'transit') {
		urls = `${urlPrefix}${type}/integrated`;
	}
	const result = await axios.get(urls, {
		params: {
			origin: start,
			destination: end,
			key: apiKey_gd,
			output: 'json',
			city: city
		}
	})
	return result;
}
/**
 * 获取附近门店
 */
async function getStoresList(params) {
	try {
		//
		// 解构请求参数
		const {
			latitude = '', longitude = '', range = 5
		} = params;

		// 检查必要参数是否为空
		if (!latitude || !longitude) {
			throw new Error('缺少必要参数');
		}
		const ip = new Ip();
		ip.set(params);
		//查询附近门店
		const result = await ip.selectStoresList(ip.get(),range);
		//
		return result;
	} catch (error) {
		throw new Error(error.message);
	}
}
/**
 * 根据经纬度查询指定门店
 */
async function getStores(params) {
	try {
		//
		// 解构请求参数
		const {
			latitude = '', longitude = ''
		} = params;

		// 检查必要参数是否为空
		if (!latitude || !longitude) {
			throw new Error('缺少必要参数');
		}
		const ip = new Ip();
		ip.set(params);
		//查询
		const result = await ip.selectStores(ip.get());
		//
		return result;
	} catch (error) {
		throw new Error(error.message);
	}
}
module.exports = {
	getIpInfo,
	getIpInfoPlus,
	suggestion,
	getRoutePlanning,
	getStoresList,
	getStores
}